package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return
     */
    @Override
    public List<AddressBook> list() {
        // 获得当前用户 userId
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder()
                .userId(userId)
                .build();
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        return addressBookList;
    }

    /**
     * 新增地址
     *
     * @param addressBook
     */
    @Override
    public void addAddressBook(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        addressBookMapper.addAddressBook(addressBook);
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @Override
    public AddressBook selectdefaultAddressBook() {
        AddressBook addressBook = AddressBook.builder().isDefault(1).build();
        List<AddressBook> list = addressBookMapper.list(addressBook);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    @Override
    public void updateAddressBookById(AddressBook addressBook) {
        addressBookMapper.updateAddressById(addressBook);
    }

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Override
    public void deleteAddressBookById(Long id) {
        addressBookMapper.deleteAddressBookById(id);
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook selectAddressBookById(Long id) {
        AddressBook addressBook = AddressBook.builder().id(id).build();
        List<AddressBook> list = addressBookMapper.list(addressBook);
        return list.get(0);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook) {
        // 获取原默认地址
        AddressBook defaultAddressBook = AddressBook.builder().isDefault(1).build();
        List<AddressBook> list = addressBookMapper.list(defaultAddressBook);
        // 将原默认地址改成普通地址
        if (list!=null&&list.size()>0){
            AddressBook defaultAddressBook2 = AddressBook.builder()
                    .id(list.get(0).getId())
                    .isDefault(0)
                    .build();
            addressBookMapper.updateAddressById(defaultAddressBook2);
        }
        // 将要设置成默认地址的地址设置成默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.updateAddressById(addressBook);
    }


}
